<template>
  <div class="login-page">
    <video
      src="../assets/login-video.mp4"
      autoplay="autoplay"
      loop="loop"
      muted="muted"
    ></video>
    <el-form :model="loginForm" :rules="rules" ref="loginForm">
      <el-form-item>
        <img src="../assets/logo.png" alt="" />
      </el-form-item>
      <el-form-item prop="account">
        <el-input
          prefix-icon="el-icon-s-custom"
          v-model="loginForm.account"
          style="width: 370px"
          placeholder="账号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="account">
        <el-input
          show-password
          prefix-icon="el-icon-unlock"
          v-model="loginForm.password"
          style="width: 370px"
          placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input
          prefix-icon="el-icon-success"
          v-model="loginForm.code"
          style="width: 370px"
          placeholder="验证码"
        >
          <div slot="suffix">
            <img
              @click="getCode()"
              class="code-image"
              :src="`https://slwl-api.itheima.net/manager/captcha?key=${loginForm.key}`"
            />
          </div>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button style="width: 370px" class="login-button" @click="login"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login } from '@/api/user'
export default {
  data () {
    return {
      loginForm: {
        account: 'shenlingadmin',
        password: '123456',
        code: '',
        key: ''
      },
      rules: {
        account: { required: true, message: '不能为空', trigger: 'blur' },
        password: { required: true, message: '不能为空', trigger: 'blur' },
        code: { required: true, message: '不能为空', trigger: 'blur' }
      }
    }
  },
  created () {
    this.getCode()
  },
  methods: {
    getCode () {
      this.loginForm.key = Math.random()
    },
    login () {
      this.$refs.loginForm.validate(async (pass) => {
        if (pass) {
          const res = await login({ ...this.loginForm })
          // console.log(res)
          // const token = res.data.token.token
          if (res.code !== 200) {
            this.$message.error(res.msg)
            this.getCode()
          } else {
            this.$message.success('登录成功')
            this.$router.push('/dashboard')
            this.$store.commit('user/updateTpken', res.data.token.token)
            this.$store.commit('user/updateUserinfo', res.data.user)
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  position: relative;
  height: 100vh;
  overflow: hidden;
  video {
    width: 100%;
    height: 100%;
    object-fit: fill;
    background-color: rgba(25, 29, 34, 0.65);
  }
}
.el-form {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 370px;
  text-align: center;
  padding: 36px 40px;
  padding-bottom: 20px;
  border-radius: 10px;
  background-color: #fff;
  img {
    width: 195px;
    height: 54px;
  }
  ::v-deep .el-input__inner {
    height: 50px;
  }
  .login-button {
    width: 100%;
    height: 54px;
    margin-bottom: 14px;
    margin-top: 22px;
    background-color: #e15536;
    border: 0;
    font-size: 18px;
    color: #fff;
    font-weight: 700;
  }
}
.code-image {
  display: inline-block;
  vertical-align: top;
  width: 71px !important;
  height: 32px !important;
  cursor: pointer;
  margin-left: 10px;
  position: absolute;
  right: 10px;
  top: 9px;
}
</style>
